package com.css.dzj.zfjg.modules.supervisionreportinfo.controller;


import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.*;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgFbComplaintService;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgFbJgjbEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgTsjbReportInfoEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.service.JgFbReportService;
import com.css.dzj.zfjg.modules.supervisionreportinfo.vo.ReportMsg;
import com.css.dzj.zfjg.modules.supervisionreportinfo.vo.ReportVo;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/lhjgjgjb/")
public class JgFbReportController {
    @Autowired
    private BaseService baseService;
    @Autowired
    JgFbReportService jgFbReportService;
    @Autowired
    JgFbComplaintService jgFbComplaintService;
    /**
     * 多条件分页查询监管投诉信息
     * @param pageSize
     * @param pageNo
     * @param tipTerritory
     * @param tipObject
     * @param fbBureau
     * @param tipType
     * @param name
     * @param taskState
     * @param receiveStartTime
     * @param receiveEndTime
     * @return
     */
    @GetMapping("listFbReportEntity")
    public R listFbReportEntity(@RequestParam Integer pageSize,
                                          @RequestParam Integer pageNo,
                                          @RequestParam String tipTerritory,
                                          @RequestParam String tipObject,
                                          @RequestParam String fbBureau,
                                          @RequestParam String fbDepartment,
                                          @RequestParam String tipType,
                                          @RequestParam String name,
                                          @RequestParam String taskState,
                                          @RequestParam (required = false) String receiveStartTime,
                                          @RequestParam (required = false) String receiveEndTime
    ) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<ReportMsg> JgTsjbComplaintInfoEntityQueryResults = jgFbReportService.getReportEntity(pageRequest,tipTerritory,tipObject,fbBureau,fbDepartment, tipType, name, taskState,receiveStartTime,receiveEndTime);
            baseService.logSuccess(3, "多条件分页查询监管举报信息成功", "2");
            return R.ok().put("data", JgTsjbComplaintInfoEntityQueryResults);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "多条件分页查询监管举报信息失败", "2");
            return R.error(e.getMessage());
        }
    }
    /**
     * 举报信息查看详情
     * 监管举报信息 举报人 任务分办  任务催办 答复内容 时间
     */

    @GetMapping("info")
    public R info(@RequestParam String registerCode) {
        try {
            //监管举报信息
            JgTsjbReportInfoEntity jgTsjbReportInfoEntity = jgFbReportService.findReportInfo(registerCode);
            //举报人信息表
            JgTsjbUserInfoEntity   jgTsjbUserInfoEntity = jgFbComplaintService.findUser(registerCode);
            //监管投诉任务分办表
            JgFbJgjbEntity jgFbJgjbEntity = jgFbReportService.findRegister(registerCode);
            //任务催办表
            JgTsjbUrgeInfoEntity jgTsjbUrgeInfoEntity = jgFbComplaintService.findCode(registerCode);
            //任务经办表（答复表）
            JgTsjbHandleInfoEntity jgTsjbHandleInfoEntity = jgFbComplaintService.fidByRegister(registerCode);
            //调解信息表
            JgTsjbMediateInfoEntity jgTsjbMediateInfoEntity = jgFbComplaintService.findMediate(registerCode);

            //回显的大VO
            ReportVo reportVo = new ReportVo();
            reportVo.setJgTsjbReportInfoEntity(jgTsjbReportInfoEntity);
            reportVo.setJgTsjbUserInfoEntity(jgTsjbUserInfoEntity);
            reportVo.setJgFbJgjbEntity(jgFbJgjbEntity);
            reportVo.setJgTsjbUrgeInfoEntity(jgTsjbUrgeInfoEntity);
            reportVo.setJgTsjbMediateInfoEntity(jgTsjbMediateInfoEntity);
            reportVo.setJgTsjbHandleInfoEntity(jgTsjbHandleInfoEntity);
            baseService.logSuccess(3, "通过任务编码获取任务详情成功", "2");
            return R.ok().put("reportVo", reportVo);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过任务编码获取任务详情失败", "2");
            return R.error(e.getMessage());
        }

    }


    /**
     * 举报信息分办页面
     * @param registerCode
     * 监管举报信息 举报人员信息
     */
    @GetMapping("getreportinfo")
    public R getreportinfo(@RequestParam String registerCode) {
        try{
            //监管举报信息
            JgTsjbReportInfoEntity jgTsjbReportInfoEntity = jgFbReportService.findReportInfo(registerCode);
            //举报人信息表
            JgTsjbUserInfoEntity   jgTsjbUserInfoEntity = jgFbComplaintService.findUser(registerCode);
            ReportVo reportVo2 = new ReportVo();
            reportVo2.setJgTsjbReportInfoEntity(jgTsjbReportInfoEntity);
            reportVo2.setJgTsjbUserInfoEntity(jgTsjbUserInfoEntity);
            return R.ok().put("reportVo2", reportVo2);
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "通过任务编码获取任务详情失败", "2");
            return R.error(e.getMessage());
        }
    }


    /**
     * 举报信息分办保存
     */
    @PostMapping("save")
    public R save(@RequestBody ReportVo reportVo, HttpServletRequest request) {
        try{
            //将分办信息入库
            JgFbJgjbEntity jgFbJgjbEntity = reportVo.getJgFbJgjbEntity();
            JgTsjbReportInfoEntity jgTsjbReportInfoEntity = reportVo.getJgTsjbReportInfoEntity();
            JgFbJgjbEntity oldJgFbJgjbEntity = jgFbReportService.findRegister(jgTsjbReportInfoEntity.getRegisterCode());
            if(oldJgFbJgjbEntity!=null){
                BeanUtil.copyBeanNotEmptyToBean(jgFbJgjbEntity, oldJgFbJgjbEntity);
                jgFbReportService.fbupdate(jgFbJgjbEntity,request);
            }else{
                jgFbJgjbEntity.setRegisterCode(jgTsjbReportInfoEntity.getRegisterCode());
                jgFbReportService.add(jgFbJgjbEntity, request);
            }
            //将监管举报信息状态改为已分办

            jgFbReportService.update(jgTsjbReportInfoEntity,request);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "提交失败", "2");
            return R.error("提交失败");
        }

    }

    /**
     * 举报信息反馈 taskState状态改为105
     */
    @GetMapping("reportfk")
    public R reportfk(@RequestParam String registerCode,HttpServletRequest request) {
        try{
            //修改反馈信息表
            JgTsjbReportInfoEntity jgTsjbReportInfoEntity = jgFbReportService.findReportInfo(registerCode);
            String flag = "105";
            jgFbReportService.updatejb(jgTsjbReportInfoEntity,request,flag);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "提交失败", "2");
            return R.error("提交失败");
        }
    }
    /**
     * 举报信息任务结束 taskState状态改为106
     */
    @GetMapping("endreport")
    public R endreport(@RequestParam String registerCode,HttpServletRequest request) {
        try{
            //修改反馈信息表
            JgTsjbReportInfoEntity jgTsjbReportInfoEntity = jgFbReportService.findReportInfo(registerCode);
            String flag = "106";
            jgFbReportService.updatejb(jgTsjbReportInfoEntity,request,flag);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "提交失败", "2");
            return R.error("提交失败");
        }
    }

}
